﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace FPP.Custom
{
    [Serializable]
    [Flags]
    public enum SiteRoles
    { 
        User = 1 << 0,
        Admin = 1 << 1
    }

    public class CustomAuthorizeAttribute : AuthorizeAttribute
    {
        public new SiteRoles Roles;

        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            var urlH = new UrlHelper(httpContext.Request.RequestContext);

            //Handle null session
            if (httpContext == null)
                throw new ArgumentException("httpContext");

            //If the user is not authenticated
            if (!httpContext.User.Identity.IsAuthenticated)
            {
                httpContext.Response.Redirect(urlH.Action("Index", "User", new { returnURL = httpContext.Request.Path }), true);
            }

            
            //SiteRoles role = (SiteRoles)httpContext.Request["role"];
            //if (Roles != 0 && ((Roles & role) != role))
            //    return false;

            return base.AuthorizeCore(httpContext);
        }
    }

}